SUBROUTINE probemaxtemp(imun,iage,Agrid,ageN)
  USE commonvars
  IMPLICIT NONE
  INCLUDE 'mpif.h'

  INTEGER, INTENT(IN) :: imun,iage, ageN, Agrid(ageN)
  INTEGER             :: agei, ipast, ifu, ipr, el_ap, ifine, iterm, ia, iedu, iwealth, ipau

  !We compute the probabilities to use in emaxtemp here so that we can do it only once
  ALLOCATE(probemax1(Neduc,Nnterms,Nfines,Nelap,Npr,Nfunds,Npast,Npaudits,Nab,Nwealth),&
           probemax2(Neduc,Nnterms,Nelap,Npast,Npaudits,Nab,Nwealth),&
           probemax3(ageN,Neduc,Nnterms,Nelap,Npast,Npaudits,Nab,Nwealth),&
           probemax4(ageN,Neduc,Nnterms,Nelap,Npast,Npaudits,Nab,Nwealth))
  
  probemax1 = 0d0
  probemax2 = 0d0
  probemax3 = 0d0
  probemax4 = 0d0

  DO iwealth = 1, Nwealth
     DO ia = 1, Nab
        DO ipau = 1, Npaudits
           DO ipast = 1, Npast
              DO ifu = 1, Nfunds
                 DO ipr = 1, Npr
                    DO el_ap = 1, Nelap
                       DO ifine = 1, Nfines
                          DO iterm = 1, Nnterms
                             DO iedu = 1, Neduc
                                
                                ! We don't need to keep track of age because age is fixed when we compute the expected value function of an incumbent
                                probemax1(iedu,iterm,ifine,el_ap,ipr,ifu,ipast,ipau,ia,iwealth) = Pr_i_win(imun,iage,ipast,el_ap,iedu,iwealth,ia,ipau,iterm)*nonpardist_fu_pr(ifu,ipr)*pfine(ifine)

!!$                                IF (myrank == 0) write(*,'(a,4f20.10)') 'probemax1', probemax1(iedu,iterm,ifine,el_ap,ipr,ifu,ipast,ipau,ia,iwealth), Pr_i_win(imun,iage,ipast,el_ap,iedu,iwealth,ia,ipau,iterm), nonpardist_fu_pr(ifu,ipr), pfine(ifine)
                                
                             END DO
                          END DO
                       END DO
                    END DO
                 END DO
              END DO
           END DO
        END DO
     END DO
  END DO

  DO iwealth = 1, Nwealth
     DO ia = 1, Nab
        DO ipau = 1, Npaudits
           DO ipast = 1, Npast
              DO el_ap = 1, Nelap
                 DO iterm = 1, Nnterms
                    DO iedu = 1, Neduc
                       
                       probemax2(iedu,iterm,el_ap,ipast,ipau,ia,iwealth) = 1d0-Pr_i_win(imun,iage,ipast,el_ap,iedu,iwealth,ia,ipau,iterm)

!!$                       IF (myrank == 0) write(*,'(a,2f20.10)') 'probemax2', probemax2(iedu,iterm,el_ap,ipast,ipau,ia,iwealth), Pr_i_win(imun,iage,ipast,el_ap,iedu,iwealth,ia,ipau,iterm)
                       
                    END DO
                 END DO
              END DO
           END DO
        END DO
     END DO
  END DO
  
  DO iwealth = 1, Nwealth
     DO ia = 1, Nab
        DO ipau = 1, Npaudits
           DO ipast = 1, Npast
              DO el_ap = 1, Nelap
                 DO iterm = 1, Nnterms
                    DO iedu = 1, Neduc
                       DO agei = 1, ageN
                       
                          ! Here we need to keep track of age because it is the age of the incumbent currently in power and not the individual we are considerin
                          probemax3(agei,iedu,iterm,el_ap,ipast,ipau,ia,iwealth) = Pr_i_win(imun,Agrid(agei),ipast,el_ap,iedu,iwealth,ia,ipau,iterm)

                       END DO
                    END DO
                 END DO
              END DO
           END DO
        END DO
     END DO
  END DO

  DO iwealth = 1, Nwealth
     DO ia = 1, Nab
        DO ipau = 1, Npaudits
           DO ipast = 1, Npast
              DO el_ap = 1, Nelap
                 DO iterm = 1, Nnterms
                    DO iedu = 1, Neduc
                       DO agei = 1, ageN
                       
                          ! We compute the probability that an incumbent loses the election
                          probemax4(agei,iedu,iterm,el_ap,ipast,ipau,ia,iwealth) = 1d0 - Pr_i_win(imun,Agrid(agei),ipast,el_ap,iedu,iwealth,ia,ipau,iterm)

                       END DO
                    END DO
                 END DO
              END DO
           END DO
        END DO
     END DO
  END DO

END SUBROUTINE probemaxtemp
